﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Coding4Fun.Kinect.Wpf;
using Microsoft.Kinect;
using Forms = System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows;
using System.ComponentModel;

namespace Mouse_and_PowerPoint_Control
{
    public class MouseControl
    {
        [DllImport("user32.dll")]
        private static extern void mouse_event(UInt32 flags, UInt32 mouse_x, UInt32 mouse_y, UInt32 data, IntPtr extraInfo);

        [Flags]
        public enum MouseEventFlags : uint
        {
            LEFTDOWN = 0x00000002,
            LEFTUP = 0x00000004,
            MIDDLEDOWN = 0x00000020,
            MIDDLEUP = 0x00000040,
            MOVE = 0x00000001,
            ABSOLUTE = 0x00008000,
            RIGHTDOWN = 0x00000008,
            RIGHTUP = 0x00000010
        }

        private static bool AlreadyAbove = false;
        private static bool AlreadyLeft = false;

        public static void UpdateMouse(Joint rightHand, Point leftHandPoint, Point shoulderLeftPoint, float SkeletonMaxX, float SkeletonMaxY)
        {
            // Gets the width and height of the user's screen
            double screenWidth = SystemParameters.PrimaryScreenWidth;
            double screenHeight = SystemParameters.PrimaryScreenHeight;

            // Scales right hand joint position to the screen resolution (ScaleTo comes from Coding4Fun library)
            Joint scaledRight = rightHand.ScaleTo((int)screenWidth, (int)screenHeight, SkeletonMaxX, SkeletonMaxY);


            uint positionX = (uint)((scaledRight.Position.X * 65535) / screenWidth);
            uint positionY = (uint)((scaledRight.Position.Y * 65535) / screenHeight);

            mouse_event((uint)MouseEventFlags.ABSOLUTE | (uint)MouseEventFlags.MOVE, positionX, positionY, 0, new System.IntPtr());

            DetermineLeftHandGesture(leftHandPoint, shoulderLeftPoint);
        }


        private static void DetermineLeftHandGesture(Point leftHandPoint, Point shoulderLeftPoint)
        {
            if (leftHandPoint.Y < shoulderLeftPoint.Y - 40)
            {
                if (!AlreadyAbove)
                {
                    SendDoubleClick();
                    AlreadyAbove = true;
                }
            }
            else
            {
                if (AlreadyAbove)
                {
                    AlreadyAbove = false;
                }
            }

            if (leftHandPoint.X < shoulderLeftPoint.X - 80)
            {
                if (!AlreadyLeft)
                {
                    SendClick();
                    AlreadyLeft = true;
                }
            }
            else
            {
                if (AlreadyLeft)
                {
                    AlreadyLeft = false;
                }
            }
        }

        internal static void SendDoubleClick()
        {
            SendClick();
            SendClick();
        }

        internal static void SendClick()
        {
            SendDown();
            SendUp();
        }

        internal static void SendUp()
        {
            mouse_event((int)MouseEventFlags.LEFTUP, 0, 0, 0, new System.IntPtr());
        }
        internal static void SendDown()
        {
            mouse_event((int)MouseEventFlags.LEFTDOWN, 0, 0, 0, new System.IntPtr());
        }
    }
}
